//
//  LJBaseClass.m
//
//  Created by   on 2018/9/28
//  Copyright (c) 2018 __MyCompanyName__. All rights reserved.
//

#import "LJBaseClass.h"
#import "LJData.h"


NSString *const kLJBaseClassErrno = @"errno";
NSString *const kLJBaseClassData = @"data";
NSString *const kLJBaseClassRequestId = @"request_id";
NSString *const kLJBaseClassError = @"error";
NSString *const kLJBaseClassUniqId = @"uniq_id";


@interface LJBaseClass ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation LJBaseClass

@synthesize errnn = _errno;
@synthesize data = _data;
@synthesize requestId = _requestId;
@synthesize error = _error;
@synthesize uniqId = _uniqId;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.errnn = [[self objectOrNilForKey:kLJBaseClassErrno fromDictionary:dict] doubleValue];
    NSObject *receivedLJData = [dict objectForKey:kLJBaseClassData];
    NSMutableArray *parsedLJData = [NSMutableArray array];
    
    if ([receivedLJData isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedLJData) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedLJData addObject:[LJData modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedLJData isKindOfClass:[NSDictionary class]]) {
       [parsedLJData addObject:[LJData modelObjectWithDictionary:(NSDictionary *)receivedLJData]];
    }

    self.data = [NSArray arrayWithArray:parsedLJData];
            self.requestId = [self objectOrNilForKey:kLJBaseClassRequestId fromDictionary:dict];
            self.error = [self objectOrNilForKey:kLJBaseClassError fromDictionary:dict];
            self.uniqId = [self objectOrNilForKey:kLJBaseClassUniqId fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.errnn] forKey:kLJBaseClassErrno];
    NSMutableArray *tempArrayForData = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.data) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForData addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForData addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForData] forKey:kLJBaseClassData];
    [mutableDict setValue:self.requestId forKey:kLJBaseClassRequestId];
    [mutableDict setValue:self.error forKey:kLJBaseClassError];
    [mutableDict setValue:self.uniqId forKey:kLJBaseClassUniqId];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.errnn = [aDecoder decodeDoubleForKey:kLJBaseClassErrno];
    self.data = [aDecoder decodeObjectForKey:kLJBaseClassData];
    self.requestId = [aDecoder decodeObjectForKey:kLJBaseClassRequestId];
    self.error = [aDecoder decodeObjectForKey:kLJBaseClassError];
    self.uniqId = [aDecoder decodeObjectForKey:kLJBaseClassUniqId];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeDouble:_errno forKey:kLJBaseClassErrno];
    [aCoder encodeObject:_data forKey:kLJBaseClassData];
    [aCoder encodeObject:_requestId forKey:kLJBaseClassRequestId];
    [aCoder encodeObject:_error forKey:kLJBaseClassError];
    [aCoder encodeObject:_uniqId forKey:kLJBaseClassUniqId];
}

- (id)copyWithZone:(NSZone *)zone {
    LJBaseClass *copy = [[LJBaseClass alloc] init];
    
    
    
    if (copy) {

        copy.errnn = self.errnn;
        copy.data = [self.data copyWithZone:zone];
        copy.requestId = [self.requestId copyWithZone:zone];
        copy.error = [self.error copyWithZone:zone];
        copy.uniqId = [self.uniqId copyWithZone:zone];
    }
    
    return copy;
}


@end
